package com.ganzalang.gmall.designpatterns.singleton.lazyload;

import lombok.extern.slf4j.Slf4j;

/**
 * @description: 加volatile禁止指令重排序
 * @author:  ganzalang
 */
@Slf4j
public class VolatileDoubleCheckLazyLoadSingleton {
    private volatile static VolatileDoubleCheckLazyLoadSingleton singleton;

    private VolatileDoubleCheckLazyLoadSingleton() {

    }

    public static VolatileDoubleCheckLazyLoadSingleton getInstance() {
        if (singleton == null) { // 1
            synchronized (DoubleCheckLazyLoadSingleton.class) { // 2
                if (singleton == null) { // 3
                    singleton = new VolatileDoubleCheckLazyLoadSingleton(); // 4
                }
            }
        }
        return singleton; // 5
    }
}
